﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Qianwei.Common;

namespace Qianwei.Web.ueditor.dialogs.image
{
    public partial class up : System.Web.UI.Page
    {
        String uploadPath = "uploadfiles/";   //保存路径
        String fileType = ".jpg,.jpeg,.gif,.png,.bmp";   //文件允许格式
        Int32 fileSize = 1000;    //文件大小限制，单位KB
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                HttpPostedFile oFile = Request.Files[0];
                string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower();
                if (fileType.ToLower().IndexOf(fileExtension) > -1)//检测是否为允许的上传文件类型
                {
                    if (this.fileSize * 1024 >= oFile.ContentLength)
                    {
                        try
                        {
                            string DirectoryPath;
                            DirectoryPath = uploadPath + DateTime.Now.ToString("yyyy-MM");
                            string sFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");  //文件名称
                            string FullPath = "~/" + DirectoryPath + "/" + sFileName + fileExtension;//最终文件路径
                            if (!Directory.Exists(Server.MapPath("~/" + DirectoryPath)))
                                Directory.CreateDirectory(Server.MapPath("~/" + DirectoryPath));
                            oFile.SaveAs(Server.MapPath(FullPath));
                            Response.Write("<script type='text/javascript'>parent.reloadImg('" + Page.ResolveUrl(FullPath) + "');" + "location.href='upload.aspx?url=" + Page.ResolveUrl(FullPath) + "';</script>");

                        }
                        catch (Exception ex)
                        {
                            Util.PageAlertAndRedirectTo("上传文件失败。", "upload.aspx");
                        }
                    }
                    else
                    {
                        Util.PageAlertAndRedirectTo("上传文件大小超过限制。", "upload.aspx");
                    }
                }
                else
                {
                    Util.PageAlertAndRedirectTo("上传文件扩展名是不允许的扩展名。", "upload.aspx");
                }
            }
        }
    }
}